﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spread.Common.Features.Sample;
using GrapeCity.CalcEngine;

namespace Spread.Calculation.Features
{
     public partial class QRCodeFunctionVisualization : BaseSampleControl
     {
          public QRCodeFunctionVisualization()
          {
               InitializeComponent();
               QRCodeFunction();
          }

          private void QRCodeFunction()
          {
               fpSpread1.AddCustomFunction(new VisualFunction("QRCode", 1, 2, FunctionAttributes.Variant, new QRCodeFunctionVisualizer()));
               var worksheet = fpSpread1.AsWorkbook().ActiveSheet;
               worksheet.Cells["B1"].Value = "https://developer.mescius.com/spread";
               worksheet.Cells["C1"].Value = "L";
               worksheet.Cells["A1"].Formula = "VF.QRCode(B1, C1)";
               worksheet.Columns[0].ColumnWidth = 200;
               worksheet.Rows[0].RowHeight = 200;
          }
     }
}

//
//QRCodeFunctionVisualizer.cs
//

using FarPoint.Win.Spread.Visualization;
using GrapeCity.CalcEngine;
using GrapeCity.Spreadsheet;
using QRCoder;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static QRCoder.QRCodeGenerator;

namespace Spread.Calculation.Features
{
  public class QRCodeFunctionVisualizer : FunctionVisualizer
  {
    protected override void PaintCell(Graphics graphics, Rectangle rect, object cellValue, ref StyleFormat styleFormat, IWorksheet worksheet)
    {
      ECCLevel eccLevel = ECCLevel.L;
      bool forceUtf8 = false;
      bool utf8BOM = false;
      EciMode eciMode = 0;
      int requestedVersion = -1;
      if (cellValue is VisualizationData visualizationData)
      {
        IEvaluationContext context = worksheet.Workbook.WorkbookSet.CalculationEngine.EvaluationContext;
        string level = visualizationData.Parameters[0].GetText(context);
        eccLevel = (QRCodeGenerator.ECCLevel)(level == "L" ? 0 : level == "M" ? 1 : level == "Q" ? 2 : 3);

        using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
        {
          using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(visualizationData.Value.GetText(context), eccLevel, forceUtf8, utf8BOM, (EciMode)eciMode, requestedVersion))
          {
            using (QRCode qrCode = new QRCode(qrCodeData))
            {
              using (Bitmap image = qrCode.GetGraphic(20, System.Drawing.Color.Black, System.Drawing.Color.White, null, rect.Width))
              {
                graphics.DrawImage(image, rect);
              }
            }
          }
        }
      }
    }
  }
}

//
//QRCodeFunctionVisualization.Designer.cs
//

namespace Spread.Calculation.Features
{
     partial class QRCodeFunctionVisualization
     {
          /// <summary> 
          /// Required designer variable.
          /// </summary>
          private System.ComponentModel.IContainer components = null;

          /// <summary> 
          /// Clean up any resources being used.
          /// </summary>
          /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
          protected override void Dispose(bool disposing)
          {
               if (disposing && (components != null))
               {
                    components.Dispose();
               }
               base.Dispose(disposing);
          }

          #region Component Designer generated code

          /// <summary> 
          /// Required method for Designer support - do not modify 
          /// the contents of this method with the code editor.
          /// </summary>
          private void InitializeComponent()
          {
               System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(QRCodeFunctionVisualization));
               this.fpSpread1 = new FarPoint.Win.Spread.FpSpread(FarPoint.Win.Spread.LegacyBehaviors.None, ((object)(resources.GetObject("pnlMain.Controls"))));
               this.fpSpread1_Sheet1 = this.fpSpread1.GetSheet(0);
               this.formulaTextBox1 = new FarPoint.Win.Spread.FormulaTextBox();
               this.pnlMain.SuspendLayout();
               ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).BeginInit();
               this.SuspendLayout();
               // 
               // pnlMain
               // 
               this.pnlMain.Controls.Add(this.formulaTextBox1);
               this.pnlMain.Controls.Add(this.fpSpread1);
               // 
               // fpSpread1
               // 
               this.fpSpread1.AccessibleDescription = "fpSpread1, Sheet1, Row 0, Column 0";
               this.fpSpread1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
               this.fpSpread1.BackColor = System.Drawing.SystemColors.Control;
               this.fpSpread1.Font = new System.Drawing.Font("Calibri", 11F);
               this.fpSpread1.Location = new System.Drawing.Point(1, 24);
               this.fpSpread1.Name = "fpSpread1";
               this.fpSpread1.RightToLeft = System.Windows.Forms.RightToLeft.No;
               this.fpSpread1.Size = new System.Drawing.Size(476, 463);
               this.fpSpread1.TabIndex = 2;
               // 
               // formulaTextBox1
               // 
               this.formulaTextBox1.Dock = System.Windows.Forms.DockStyle.Top;
               this.formulaTextBox1.Location = new System.Drawing.Point(0, 0);
               this.formulaTextBox1.Name = "formulaTextBox1";
               this.formulaTextBox1.Size = new System.Drawing.Size(477, 23);
               this.formulaTextBox1.TabIndex = 3;
               // Attach formulaTextBox1 to fpSpread1
               this.formulaTextBox1.Attach(this.fpSpread1);
               // 
               // QRCodeFunctionVisualization
               // 
               this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
               this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
               this.Name = "Custom Data Visualization";
               this.pnlMain.ResumeLayout(false);
               ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).EndInit();
               this.ResumeLayout(false);

          }

          #endregion

          private FarPoint.Win.Spread.FormulaTextBox formulaTextBox1;
          private FarPoint.Win.Spread.FpSpread fpSpread1;
          private FarPoint.Win.Spread.SheetView fpSpread1_Sheet1;
     }
}
